package com.wang.services;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

public class StateDAOImpl implements StateDAO {

	private Session session;

	public StateDAOImpl(Session session) {
		this.session = session;
	}

	/**
	 * 执行了未检查的转换时的警告，例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型
	 * */
	@SuppressWarnings("unchecked")
	public <T, PK extends Serializable> T findByID(Class<T> type, PK id) {
		// TODO Auto-generated method stub
		return (T) session.get(type, id);
	}

	public <T> T create(T t) {
		session.persist(t);
		session.flush();
		session.refresh(t);
		return t;
		
	}

	public <T> T update(T t) {
		session.merge(t);
		return t;
	}

	public <T, PK extends Serializable> void deleteByID(Class<T> type, PK id) {
		T t = (T)session.get(type, id);
		session.delete(t);
		

	}

	public <T> List<T> findWithNameQuery(String queryName, int num) {
		return session.createQuery(queryName).setMaxResults(num).list();
		 
	}

	public <T> List<T> findWithQuery(String queryName) {
		// TODO Auto-generated method stub
		return session.createQuery(queryName).list();
	}

}
